package com.pengpeng.dynamic.config;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author 彭鹏
 * @date 2021/11/4.
 * 动态数据源上下文
 */
public class DynamicDataSourceContextHolder {
    private static final ThreadLocal<String> contextHolder = ThreadLocal.withInitial(() -> "master");

    public static List<Object> dataSourceKeys = new ArrayList<>();

    public static void setDataSourceKey(String key){
        contextHolder.set(key);
    }

    public static String getDataSourceKey(){
        return contextHolder.get();
    }

    public static void clearDataSourceKey(){
        contextHolder.remove();
    }

    public static boolean containDataSourceKey(String key){
        return dataSourceKeys.contains(key);
    }

    public static boolean addDataSourceKeys(Collection<? extends Object> keys){
        return dataSourceKeys.addAll(keys);
    }
}
